package org.dey.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.aspectj.weaver.ast.Or;
import org.dey.entity.Result;
import org.dey.mapper.VisitDiaryMapper;
import org.dey.pojo.*;
import org.dey.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author deyran
 * @classname
 * @see ServiceImpl
 */
@Service
@Transactional
public class VisitDiaryServiceImpl extends ServiceImpl<VisitDiaryMapper, VisitDiary> implements VisitDiaryService {

    @Autowired
    VisitDiaryMapper visitDiaryMapper;
    @Autowired
    UserService userService;
    @Autowired
    MemberService memberService;
    @Autowired
    RenqunfeileiService renqunfeileiService;


    @Override
    public PageVo<VisitDiary> queryvisitdiary(VisitDiary visitdiary, int pageNum, int pagesize) {

        //组装条件
        QueryWrapper<VisitDiary> queryWrapper = new QueryWrapper<VisitDiary>();
        //根据档案号模糊查询//根据手机号模糊查询//根据名字模糊查询
/*        if(visitdiary.getMid()!=-1) {
            queryWrapper.eq("mid", visitdiary.getMid());
        }
        if(visitdiary.getUid()!=-1) {
            queryWrapper.eq("uid", visitdiary.getUid());
        }*/

        PageHelper.startPage(pageNum, pagesize);
        List<VisitDiary> visitdiaryinfos = this.list(queryWrapper);

        for (VisitDiary visitdiaryinfo : visitdiaryinfos) {
            Integer mid = visitdiaryinfo.getMid();
            Member member = memberService.getById(mid);
            visitdiaryinfo.setMname(member.getName());
            Integer uid = visitdiaryinfo.getUid();
            User user = userService.getById(uid);
            visitdiaryinfo.setUname(user.getUsername());
        }

        PageInfo<VisitDiary> pageInfo = new PageInfo<VisitDiary>(visitdiaryinfos);

        PageVo<VisitDiary> pageVo = new PageVo<VisitDiary>();
        pageVo.setRows(pageInfo.getList());
        pageVo.setTotal(pageInfo.getTotal());

        return pageVo;
    }


    @Override
    public Result insertvisitdiary(VisitDiary visitdiary) {
        Result result = null;
        Member member = visitDiaryMapper.getmember(visitdiary.getMid());
        visitdiary.setFilenumber(member.getFileNumber());
        visitdiary.setMname(member.getName());

        QueryWrapper<User> queryWrapper2 = new QueryWrapper<User>();
        queryWrapper2.eq("id", visitdiary.getUid());
        User user = userService.getOne(queryWrapper2);
        visitdiary.setUname(user.getUsername());
        UpdateWrapper<RenQunFenLei> updateWrapper = new UpdateWrapper<RenQunFenLei>();
        updateWrapper.eq("id",visitdiary.getMid()).set("zhuangtai","已随访");
        boolean update = renqunfeileiService.update(updateWrapper);
        if (update){

            visitdiary.setMid(member.getId());

            boolean one = this.save(visitdiary);
            if (one) {
                result = new Result(true, "随访日记增加成功");
            } else {
                result = new Result(true, "随访日记增加失败");
            }
        }else {
            result = new Result(true, "随访日记增加失败");
        }
        //前端弹框取值==> res.data.message
        return result;
    }

    @Override
    public Result delvisitdiary(int id) {
        Result result = null;
        boolean b = this.removeById(id);
        if (b) {
            result = new Result(true, "随访日记删除成功");
        } else {
            result = new Result(true, "随访日记删除失败");
        }
        return result;
    }

    @Override
    public VisitDiary queryvisitdiarybyid(int id) {
        return this.getById(id);
    }

    @Override
    public Result updatevisitdiarybyid(VisitDiary visitdiary) {
        Result result = null;
        boolean b = this.updateById(visitdiary);
        if (b) {
            result = new Result(true, "随访日记修改成功");
        } else {
            result = new Result(true, "随访日记修改失败");
        }
        return result;
    }


    @Override
    public List<RenQunFenLei> getmemberName() {

        //组装条件
        List<RenQunFenLei> orders = visitDiaryMapper.getmid();
        for (RenQunFenLei order : orders) {
            Member member = memberService.getById(order.getMember());
            order.setMname(member.getName());
        }
        return orders;
    }


    @Override
    public List<User> getUserRole() {
        List<User> users = new ArrayList<User>();
        List<Integer> uids = visitDiaryMapper.queryallUserRole();
        for (Integer uid : uids) {
            User user = userService.getById(uid);
            users.add(user);
        }
        return users;
    }

}
